package ar.com.caeycae.service;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import ar.com.caeycae.data.Movimiento;


@Component
public class MovimientoService implements Serializable {

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Transactional
	public Movimiento agregar(Movimiento movimiento){
		Session session = sessionFactory.getCurrentSession();
		long id = (long) session.save(movimiento);
		movimiento.setId(id);
		return movimiento;
	}

	@Transactional
	public void actualizar(Movimiento movimiento){
		Session session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(movimiento);
	}

	@Transactional
	public void borrar(Movimiento movimiento){
		Session session = sessionFactory.getCurrentSession();
		session.delete(movimiento);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public List<Movimiento> listar(){
		return sessionFactory.getCurrentSession().createCriteria(Movimiento.class).list();
	}

}
